package org.example.blog.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.blog.pojo.History;
import org.example.blog.pojo.Result;
import org.example.blog.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/history")
public class HistoryController {
    @Autowired
    private HistoryService historyService;

    // 查询所有历史记录
    @GetMapping
    public Result list() {
        log.info("查询所有历史记录");
        List<History> historyList = historyService.list();
        return Result.success(historyList);
    }

    // 模糊查询
    @GetMapping("/mohu")
    public Result search(@RequestParam String name) {
        log.info("模糊查询历史记录，关键字：{}", name);
        List<History> historyList = historyService.search(name);
        return Result.success(historyList);
    }

    // 新增历史记录
    @PostMapping
    public Result add(@RequestBody History history) {
        log.info("新增历史记录：{}", history);
        historyService.add(history);
        return Result.success();
    }

    // 删除历史记录
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除历史记录，id：{}", id);
        historyService.delete(id);
        return Result.success();
    }
}